package com.hb0730.boot.admin.modules.sci.controller;

import com.hb0730.boot.admin.base.R;
import com.hb0730.boot.admin.base.util.JwtUtil;
import com.hb0730.boot.admin.data.domain.BasePage;
import com.hb0730.boot.admin.modules.sci.model.entity.SciWorkLearning;
import com.hb0730.boot.admin.modules.sci.model.query.SciWorkLearningQuery;
import com.hb0730.boot.admin.modules.sci.service.SciWorkLearningService;
import com.hb0730.boot.admin.security.model.UserInfo;
import com.hb0730.boot.admin.security.service.UserDetailServiceImpl;
import com.hb0730.boot.admin.security.token.RedisCacheUserProvider;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/sci/work/learning")
@Tag(name = "科研：著作管理：学术著作")
@RequiredArgsConstructor
public class SciWorkLearningController {

    private final SciWorkLearningService sciWorkLearningService;
    private final RedisCacheUserProvider redisCacheUserProvider;
    private final UserDetailServiceImpl userDetailService;
    

    @GetMapping("/query/page")
    @Operation(summary = "分页查询")
    public R<BasePage<SciWorkLearning>> queryPage(HttpServletRequest request, @ParameterObject SciWorkLearningQuery query) {
        System.out.println(query);
        BasePage<SciWorkLearning> res = this.sciWorkLearningService.queryPage(query);
        return R.OK(res);
    }

    @PostMapping("/save")
    @Operation(summary = "保存")
    public R<SciWorkLearning> save(HttpServletRequest request, @Validated @RequestBody SciWorkLearning sciWorkLearning) {
        System.out.println(sciWorkLearning);
        String _jwtToken = JwtUtil.getTokenByRequest(request);
        Optional<UserInfo> optional = redisCacheUserProvider.getUser(JwtUtil.getUsername(_jwtToken));
        UserInfo userInfo = optional.get();
        sciWorkLearning.setDepartment(userInfo.getDepartment());
        sciWorkLearning.setUsername(userInfo.getUsername());
        sciWorkLearning.setNickname(userInfo.getNickname());
        sciWorkLearning.setStatus(1);
        System.out.println(sciWorkLearning);
        return this.sciWorkLearningService.saveLearning(sciWorkLearning);
    }

    @PutMapping("/update/{id}")
    @Operation(summary = "修改")
    public R<SciWorkLearning> update(HttpServletRequest request, @PathVariable String id,
                                        @Validated @RequestBody SciWorkLearning sciWorkLearning) {
        return this.sciWorkLearningService.updateById(id, sciWorkLearning);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    public R<String> delete(HttpServletRequest request, @Validated @RequestBody List<String> ids) {
        if (null == ids || ids.isEmpty()) {
            return R.NG("删除失败，缺少参数");
        }
        return this.sciWorkLearningService.deleteByIds(ids);
    }
}
